package com.orhon.smartcampus.modules.teacher.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.teacher.dao.ProfessionalQualificationDao;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.ProfessionalQualification;
import com.orhon.smartcampus.modules.teacher.gqlInputModel.GQLProfessionalQualificationInput;
import com.orhon.smartcampus.modules.teacher.inputMapper.ProfessionalQualificationInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddProfessionalQualificationMutation implements DataFetcher {

    @Autowired
    ProfessionalQualificationDao professionalQualificationDao;

    @Autowired
    TInformationDao tInformationDao;

    @Autowired
    SchoolsDao schoolsDao;


    @Autowired
    TokenService tokenService;
    /**
     * 添加专业技能资格
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();

        Long schoolId = JwtUtils.getSchoolId(token);
        GQLProfessionalQualificationInput input = ProfessionalQualificationInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        ProfessionalQualification professionalQualification = null;

        if (input.getId() != null) {
            professionalQualification = professionalQualificationDao.get(ProfessionalQualification.class, input.getId());
            professionalQualification.setUpdatedAt(new Date());
        } else {
            professionalQualification = new ProfessionalQualification();
            professionalQualification.setCreatedAt(new Date());
        }
        if(schoolId != null){

            professionalQualification.setSchool_id(schoolId);
        }
        if(input.getTeacher_id() != null){

            professionalQualification.setTeacher_id(input.getTeacher_id());
        }
        if(input.getExperttype() != null){

            professionalQualification.setExperttype(input.getExperttype());
        }
        if(input.getTradeid() != null){

            professionalQualification.setTradeid(input.getTradeid());
        }
        if(input.getInputstyle() != null){

            professionalQualification.setInputstyle(input.getInputstyle());
        }
        if(input.getJobtypeid() != null){

            professionalQualification.setJobtypeid(input.getJobtypeid());
        }
        if(input.getTakedate() != null){

            professionalQualification.setTakedate(input.getTakedate());
        }
        if(input.getJtlevelid() != null){

            professionalQualification.setJtlevelid(input.getJtlevelid());
        }
        if(input.getCertificateno() != null){

            professionalQualification.setCertificateno(input.getCertificateno());
        }
        if(input.getImage() != null){

            professionalQualification.setImage(input.getImage());
        }
        if(input.getIsemploy() != null){

            professionalQualification.setIsemploy(input.getIsemploy());
        }
        if(input.getShenhe() != null){

            professionalQualification.setShenhe(input.getShenhe());
        }
        if(input.getAwarddept() != null){

            professionalQualification.setAwarddept(JSON.toJSONString(input.getAwarddept()));
        }
        try{
            professionalQualificationDao.saveOrUpdate(professionalQualification);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

